﻿#region

using System.IO;
using NLog;
using NLog.Common;
using NLog.Config;
using NLog.Targets;

#endregion

namespace MediaSage.Lib.Client.Config
{
    public class Logger
    {
        #region Fields

        private static NLog.Logger log;

        private readonly Configuration config;
        private readonly LoggingConfiguration logConfig;

        #endregion Fields

        #region Constructors

        ///<summary>
        ///    Sets up the default logger
        ///
        ///    Log levels are  0 - All
        ///    1 - Warn
        ///    2 - Error
        ///</summary>
        public Logger(Configuration Configuration)
        {
            config = Configuration;
            logConfig = new LoggingConfiguration();
            log = LogManager.GetLogger("MediaSageClient");

            if (Configuration.LogLevel > 2 || Configuration.LogLevel < 0)
            {
                Configuration.LogLevel = 1;
            }

            var fileTarget = new FileTarget();
            LoggingRule logRule;
#if DEBUG
            logRule = new LoggingRule("*", LogLevel.Info, fileTarget);
            logConfig.LoggingRules.Add(logRule);
#else
            switch (Configuration.LogLevel)
            {
                case 0:
                    logRule = new LoggingRule("*", LogLevel.Info, fileTarget);
                    logConfig.LoggingRules.Add(logRule);
                    break;

                case 1:
                    logRule = new LoggingRule("*", LogLevel.Warn, fileTarget);
                    logConfig.LoggingRules.Add(logRule);
                    break;

                case 2:
                    logRule = new LoggingRule("*", LogLevel.Error, fileTarget);
                    logConfig.LoggingRules.Add(logRule);
                    break;

                default:
                    logRule = new LoggingRule("*", LogLevel.Warn, fileTarget);
                    logConfig.LoggingRules.Add(logRule);
                    break;
            }
#endif

            logConfig.AddTarget("logfile", fileTarget);
            if (config.LogFile != null)
            {
                fileTarget.FileName = config.LogFile;
            }
            fileTarget.Layout = "${date}: ${level} - ${message}";
            fileTarget.KeepFileOpen = true;
            fileTarget.ArchiveAboveSize = 1024 * 1024 * 20;
            fileTarget.Name = "FileTarget";
            fileTarget.FileName = config.LogFile;

#if DEBUG
            LogManager.ThrowExceptions = true;
            InternalLogger.LogFile = Path.Combine(Configuration.BaseDirectory, "nLog.log");
            InternalLogger.LogLevel = LogLevel.Trace;
#endif

            LogManager.Configuration = logConfig;
        }

        #endregion Constructors

        #region Enumerations

        public enum Levels
        {
            INFO,
            WARN,
            ERROR
        }

        #endregion Enumerations

        #region Methods

        public void Log(Levels LoggingLevel, string Msg)
        {
            if (LoggingLevel == Levels.INFO)
            {
                log.Info(Msg);
                return;
            }
            if (LoggingLevel <= Levels.WARN)
            {
                log.Warn(Msg);
                return;
            }
            if (LoggingLevel <= Levels.ERROR)
            {
                log.Error(Msg);
                return;
            }
        }

        public void Log(string Msg)
        {
            log.Info(Msg);
        }

        #endregion Methods
    }
}